package com.cdtu.outtake.controller;

import com.cdtu.outtake.common.Result;
import com.cdtu.outtake.controller.vo.UpdateCartVO;
import com.cdtu.outtake.domain.ShopOfCart;
import com.cdtu.outtake.MyException;
import com.cdtu.outtake.domain.Cart;
import com.cdtu.outtake.service.ICartService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author : shl
 * @Date 2024/3/8 22:52
 */
@RestController
@RequestMapping("/cart")
@RequiredArgsConstructor
public class CartController {
    private final ICartService cartService;

    /**
     * 获取店铺购物车信息
     * @param shopId
     * @return
     */
    @GetMapping("/{shopId}")
    public Result<ShopOfCart> getCart(@PathVariable("shopId") Long shopId) {
        return Result.ok(cartService.getShopOfCart(shopId));
    }

    /**
     * 清空购物车
     * @param shopId
     * @return
     */
    @PostMapping("/clear/{shopId}")
    public Result<Object> clearCart(@PathVariable("shopId") Long shopId) {
        cartService.clearCartOfShop(shopId);
        return Result.ok();
    }

    /**
     * 修改购物车
     * @param updateCartVO
     * @return
     */
    @PostMapping
    public Result<ShopOfCart> updateCartData(@RequestBody UpdateCartVO updateCartVO) {
        checkData(updateCartVO);
        return Result.ok(cartService.updateCartData(updateCartVO));
    }

    private void checkData(UpdateCartVO updateCartVO) {
        if (updateCartVO.getNumber() == null ||
                updateCartVO.getProductId() == null ||
                updateCartVO.getShopId() == null)
            throw new MyException("参数异常");
    }


    @GetMapping
    public Result<Cart> getCart() {
        return Result.ok();
    }
}
